﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MISAPP.MISInterface;
using MISAPP.Classify.Service;

public partial class View_WMG_WMG006 : ViewMatrix
{
    string nextPage = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        lbl_result.Text = string.Empty;
        linkExcel.Text = string.Empty;
        linkExcel.NavigateUrl = string.Empty;
        Title = "基金庫存分析表";
        limit();
        criteriaList = new string[] { "Criteria","type" };
        serviceName = "MISAPP.Classify.Service.Service_WMG006";
        if (!IsPostBack)
        {
            FillCriteria("GetCriteriaData", "SetData");
        }
    }

    protected void btn_ExecuteClick(object sender, EventArgs e)
    {
        Button btn = sender as Button;
        //Execute
        if (!btn.Text.Equals("Execute"))
        {
            HttpContext.Current.Items.Add("top50", true);
        }
        execute();
        Server.Transfer(nextPage);        
    }

    protected void execute()
    {
        List<iSelectList> CriteriaList = GetSelectCLB();
        Session["Criteria"] = CriteriaList;

        //客群
        Session["type"] = rdo1.Checked ? Service_WMG006.FundType.Cust :
            Service_WMG006.FundType.CO;
        nextPage = string.Format("WMG006_1.aspx?node={0}",
            rdo1.Checked ? Service_WMG006.FundType.Cust.ToString() :
            Service_WMG006.FundType.CO.ToString());
          
    }

    protected void stockTopTen(object sender, EventArgs e)
    {
        List<string> ym = CLB_YYYYMM.GetSelectList();
        if (ym.Count != 2)
        {
            lbl_result.Text = "月份只能選一個";
            return;
        }
        string yyyymm = ym[1].ToString();
        string dlFile = Utility.TemplateToDownload(Server.MapPath("~")
       , "stocktopten.xls", "xls");
        Service_WMG006 wmg = new Service_WMG006();
        System.Data.DataTable dt = wmg.GetStockTopTen(yyyymm);
        string month = wmg.GetStockTopTenYearMonth(yyyymm);
        string[] months = month.Split(new char[] { ','});
        Squared_Excel excel = new Squared_Excel();
        excel.WMGStockTopTen(dt, dlFile, months);
        linkExcel.NavigateUrl = string.Format("~/download/{0}",
            System.IO.Path.GetFileName(dlFile));
        linkExcel.Text = "當月前十大基金庫存分析下載";
        excel.ReleaseExcel();
    }
}
